package common;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class imageDownload {
	public static void main(String[] args) {
		String url = "http://47.98.47.143:8080/LongSheng/upload/20180423/1524459594941ba5b9c49b2ee4028a0e849418b1eb0a7.jpg";
		downloadPicture(url);
	}

	@SuppressWarnings("unused")
	private static void downloadPicture(String urlList) {
		URL url = null;
		int imageNumber = 0;
		try {
			url = new URL(urlList);
			DataInputStream dataInputStream = new DataInputStream(
					url.openStream());

			String imageName = "/Users/jiangwei/test.jpg";

			FileOutputStream fileOutputStream = new FileOutputStream(new File(
					imageName));
			ByteArrayOutputStream output = new ByteArrayOutputStream();

			byte[] buffer = new byte[1024];
			int length;
			while ((length = dataInputStream.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
			byte[] context = output.toByteArray();
			fileOutputStream.write(output.toByteArray());
			dataInputStream.close();
			fileOutputStream.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}